package day018.thread.demo4;

import java.util.concurrent.locks.ReentrantLock;

public class Ticket implements Runnable {
    private int ticketCount = 100;
    ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            try {
                lock.lock();
                if (ticketCount <= 0) {
                    break;
                } else {
                    Thread.sleep(100);
                    ticketCount--;
                    System.out.println(Thread.currentThread().getName() + "卖出一张票，剩下" + ticketCount + "张");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }
    }
}
